package streamdef;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import event.Event;

public class EventSet {
	private StreamCondition query = null;
	private HashMap<Long, Event> internalStreamIDToEvent = null;

	public EventSet(long streamID) {
		query = StreamDefinitionManager.instance().getParsedQuery(streamID);
		internalStreamIDToEvent = new HashMap<Long, Event>();
	}

	public EventSet(StreamCondition query) {
		this.query = query;
		internalStreamIDToEvent = new HashMap<Long, Event>();
	}

	public EventSet(long streamID, HashMap<Long, Event> matchingEvents) {
		query = StreamDefinitionManager.instance().getParsedQuery(streamID);
		internalStreamIDToEvent = matchingEvents;
	}

	public EventSet(StreamCondition query, HashMap<Long, Event> matchingEvents) {
		this.query = query;
		internalStreamIDToEvent = matchingEvents;
	}

	public EventSet clone() {
		EventSet result = new EventSet(this.query);

		Iterator<Long> iteratorID = this.internalStreamIDToEvent.keySet()
				.iterator();
		while (iteratorID.hasNext()) {
			long ID = iteratorID.next();
			result.addEvent(ID, this.internalStreamIDToEvent.get(ID));
		}
		return result;
	}

	public void addEvent(long internalStreamID, Event e) {
		internalStreamIDToEvent.put(new Long(internalStreamID), e);
	}

	public long getStreamID() {
		return query.getStreamID();
	}

	public Event getEventForInternalStreamID(long ID) {
		return internalStreamIDToEvent.get(ID);
	}

	public Timestamp getMostRecentTimestamp() {
		Timestamp result = null;
		Iterator<Long> iteratorID = this.internalStreamIDToEvent.keySet()
				.iterator();
		while (iteratorID.hasNext()) {
			Long ID = iteratorID.next();
			Event e = internalStreamIDToEvent.get(ID);
			if (result == null)
				result = e.getTimestamp();
			else if (e.getTimestamp().after(result))
				result = e.getTimestamp();
		}

		return result;
	}

	public Event getMostRecentEvent() {
		Event result = null;
		Iterator<Long> iteratorID = this.internalStreamIDToEvent.keySet()
				.iterator();
		while (iteratorID.hasNext()) {
			Long ID = iteratorID.next();
			Event e = internalStreamIDToEvent.get(ID);
			if (result == null)
				result = e;
			else if (e.getTimestamp().after(result.getTimestamp()))
				result = e;
		}

		return result;
	}

	public Vector<Long> getEventIDs() {
		Vector<Long> v = new Vector<Long>();
		Iterator<Long> iteratorID = this.internalStreamIDToEvent.keySet()
				.iterator();
		while (iteratorID.hasNext()) {
			long ID = iteratorID.next();
			v.add(internalStreamIDToEvent.get(ID).getID());
		}
		if (v.size() != 0)
			return v;
		return null;
	}

	public Vector<Long> getEventIDsForStreamID(long extStreamID) {
		Vector<Long> v = new Vector<Long>();
		Iterator<Long> iteratorID = this.internalStreamIDToEvent.keySet()
				.iterator();
		while (iteratorID.hasNext()) {
			long ID = iteratorID.next();
			if (query.getExternalStreamID(ID) == extStreamID)
				v.add(internalStreamIDToEvent.get(ID).getID());
		}
		if (v.size() != 0)
			return v;
		return null;
	}
}
